package com.erji.common.utils;

import com.erji.common.exception.DefaultErrorObject;
import com.erji.common.exception.IError;
import com.erji.common.exception.ServiceException;

import java.text.MessageFormat;
import java.util.Objects;

public class Assert {
    public Assert() {
    }

    public static void isTrue(boolean expression, String message, Object... errorFormatArgs) {
        if (!expression) {
            throw new ServiceException(MessageFormat.format(message, errorFormatArgs), DefaultErrorObject.SYSTEM_SERVER_ERROR.code());
        }
    }

    public static void isTrue(boolean expression, IError error, Object... errorFormatArgs) {
        if (!expression) {
            throw new ServiceException(error, errorFormatArgs);
        }
    }

    public static void isNull(Object object, String message, Object... errorFormatArgs) {
        isTrue(Objects.isNull(object), message, errorFormatArgs);
    }

    public static void isNull(Object object, IError error, Object... errorFormatArgs) {
        isTrue(Objects.isNull(object), error, errorFormatArgs);
    }

    public static void notNull(Object object, String message, Object... errorFormatArgs) {
        isTrue(Objects.nonNull(object), message, errorFormatArgs);
    }

    public static void notNull(Object object, IError error, Object... errorFormatArgs) {
        isTrue(Objects.nonNull(object), error, errorFormatArgs);
    }
}
